 <!-- template标签，每个页面的最外层都是这个标签，这个是vue上的功能 -->
<template>
    <!-- 页面div 这是若依框架的的样式 -->
    <div class="app-container">

        <!-- 搜索区域开始，使用了ElementUI 的form表单 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <!-- 搜索条件：姓名字段 -->
            <el-form-item label="姓名" prop="regName">
                <el-input 
                v-model="queryParams.regName" 
                placeholder="请输入姓名" 
                clearable
                @keyup.enter.native="handleQuery" 
                />
            </el-form-item>

            <!-- 搜索条件，注册状态字段 -->
            <el-form-item label="状态" prop="regStatus">
                <el-select v-model="queryParams.regStatus" placeholder="请选择注册状态" clearable>
                    <el-option 
                    v-for="dict in dict.type.device_user_register_status" 
                    :key="dict.value"
                    :label="dict.label" 
                    :value="dict.value" 
                    />
                </el-select>
            </el-form-item>

            <!-- 搜索和重置按钮 -->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mimi" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>

        </el-form>
        <!-- 搜索区域结束 -->

        <!-- 按钮区域 -->
        <el-row :gutter="10" class="mb8">
            <!-- 左侧导出按钮 -->
            <el-col :span="1.5">
                <el-button 
                    type="warning" 
                    plain 
                    icon="el-icon-download" 
                    size="mini" 
                    @click="handleExport"
                    v-hasPermi="['device:deviceUserRegister:export']"
                    >导出</el-button>
            </el-col>
            <!-- 右侧刷新按钮 -->
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <!-- 按钮区域结束 -->

        <!-- 列表区域开始 -->
        <el-table v-loading="loading" :data="deviceUserRegisterList">
            <el-table-column label="所属部门" align="center" prop="regDept" />
            <el-table-column label="工号/学号" align="center" prop="regCode" />
            <el-table-column label="姓名" align="center" prop="regName" />
            <el-table-column label="注册时间" align="center" prop="createTime">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="审核状态" align="center" prop="regStatus">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.device_user_register_status" :value="scope.row.regStatus" />
                </template>
            </el-table-column>
            <el-table-column label="说明" align="center" prop="regRemark" />
            <el-table-column label="审核人" align="center" prop="user.nickName" />
            <el-table-column label="审核时间" align="center" prop="updateTime">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width ">
                <template slot-scope="scope">
                    <el-button 
                    type="text" 
                    size="mini" 
                    icon="el-icon-edit" 
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['device:deviceUserRegister:edit']" 
                    v-if="scope.row.regStatus == 0"
                    >审核</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 列表区域结束 -->

        <!-- 分页按钮区域开始 -->
        <pagination 
            v-show="total > 0" 
            :total="total" 
            :page.sync="queryParams.pageNum" 
            :limit.sync="queryParams.pageSize"
            @pagination='getList' 
            />
        <!-- 分页按钮区域结束 -->

        <!-- 弹出层开始（审核app注册用户 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" label-width="80px" :rules="rules">
              <el-form-item label="注册部门" prop="regDept">
                <el-input v-model="form.regDept" disabled/>
              </el-form-item>
              <el-form-item label="工号/学号" prop="regCode">
                <el-input v-model="form.regCode" placeholder="请输入工号或学号" disabled/>
              </el-form-item>
              <el-form-item label="姓名" prop="regName">
                <el-input v-model="form.regName" placeholder="请输入姓名" disabled/>
              </el-form-item>
              <el-form-item label="归属部门" prop="regDeptId">
                <treeselect v-model="form.regDeptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" />
              </el-form-item>
              <el-form-item label="审核状态" prop="regStatus">
                <el-radio-group v-model="form.regStatus" @change="changeExamine">
                  <el-radio
                   v-for="dict in dict.type.device_user_register_status"
                   :key="dict.value"
                   :label="parseInt(dict.value)"
                   >{{ dict.label }}</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="审核说明" prop="regRemark">
                <el-input v-model="form.regRemark" placeholder="请输入审核说明"/>
              </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确定</el-button>
                <el-button  @click="cancel">取消</el-button>
            </div>
        </el-dialog>
        <!-- 弹出层结束 -->
        
    </div>
</template>

<!--以下全部为页面功能js实现-->
<script>
// 引用齐纳后端交互js
import{ listRegister, getRegister, editRegister} from "@/api/device/deviceUserRegister";
// 一下三个引用用于实现列表数功能
import {deptTreeSelect} from '@/api/system/user';
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

// vue页面js
export default {
    name: "DeviceUserRegister", // vue页面js名称
    dicts: ['device_user_register_status'], // 引用字典
    components: {Treeselect}, // 引用列表树组件
    // 在data里面定义页面变量
    data() {
        return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // app用户注册记录表格数据
        deviceUserRegisterList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 部门树选项
        deptOptions: undefined,
        // 查询参数
        queryParams: {
            pageNum: 1,
            pageSize: 10,
            regName: null,
            regStatus: null
        },
        // 表单参数
        form: {},
        rules: {
            regDeptId: [
                { required: true, message: "归属部门不能为空", trigger: "blur"}
            ],
            regRemark: [
                { required: true, message: "审核说明不能为空", trigger: "blur"}
            ],
        }
    };
    },
    // 在created里初始化页面
    created() {
        this.getList();
        this.getDeptTree();
    },
    // 页面使用的各个功能的方法定义
    methods: {
        // 获取app注册用户列表
        getList(){
            this.loading = true;
            listRegister(this.queryParams).then(response => {
                this.deviceUserRegisterList = response.rows;
                this.total = response.total;
                this.loading = false
            });
        },
        // 获取部门列表树
        getDeptTree(){
            deptTreeSelect().then(response => {
                this.deptOptions = response.data;
            });
        },
        // 取消按钮
        cancel(){
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                regId: null,
                regDept: null,
                regCode: null,
                regName: null,
                regStatus: null,
                regRemark: null,
                crateBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            };
            this.resetForm("form")
        },
        // 搜索按钮操作
        handleQuery(){
            this.queryParams.pageNum = 1;
            this.getList();
        },
        // 搜索重置按钮操作
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 审核按钮操作
        handleUpdate(row) {
            this.reset();
            const id = row.regId;
            getRegister(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "审核app注册用户";
            });
        },
        // 提交按钮
        submitForm(){
            this.$refs["form"].validate(valid => {
                if(valid) {
                    if(this.form.regStatus == 2 && this.form.regRemark == ''){
                        this.$modal.msgError("请输入审核说明");
                        return;
                    }
                    editRegister(this.form).then(response => {
                        this.$modal.msgSuccess("保存成功");
                        this.open = false;
                        this.getList();
                    });
                }
            });
        },
        // 导出按钮操作
        handleExport(){
            this.download('device/deviceUserRegister/export',{
                ...this.queryParams
            }, `deviceUserRegister_${new Date().getTime()}.xlsx`)
        },
        // 修改审核状态
        changeExamine(val){
            if(val == "1"){
                this.form.regRemark = "审核通过";
            }else{
                this.form.regRemark = "";
            }
        }

    }

};
</script>
